package fcl.pattern;

import java.text.*;
import java.util.ArrayList;
import java.util.*;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.View;
import fcl.pattern.DataProcessing;

public class PatternPanel extends View {

	DataProcessing data;

	public PatternPanel(Context context) {
		super(context);
	}
	
	//the shared parameters, for example, the maximun value will be prepared here
	public void prepareParameters(){
		//sample code -  to get the max Distance
		double maxdis = data.getMaxDis(); 
	}
	

	@Override
	public void onDraw(Canvas canvas) {

		prepareParameters();
		drawStatic(canvas);
		drawData(canvas);

	}

	
	
	private void drawStatic(Canvas canvas) {
		// TODO Auto-generated method stub

		double maxdis = data.getMaxDis(); //DS 
		double Stime = data.getmyStartTime();
		double Etime = data.getmyEndTime();
		double Mtime = Stime + ((Etime-Stime)*0.5);
		
		Date mySTime = new Date((long) Stime);
		Date myETime = new Date((long) Etime);
		Date myMTime = new Date((long) Mtime);
		SimpleDateFormat myDateF = new SimpleDateFormat("MMM dd");
		

		Paint mPaint = new Paint();
		//mPaint.setDither(true);
		// mPaint.setColor(Color.RED);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		Typeface tfB = Typeface.create("Tahoma",Typeface.BOLD);
		Typeface tf = Typeface.create("Tahoma",Typeface.NORMAL);
		
		// mPaint.setStrokeWidth(3);

		// draw the begining red
		mPaint.setARGB(150, 1, 1, 1);
		;
		mPaint.setStrokeWidth(1);
		canvas.drawLine(10, this.getHeight() / 20, this.getWidth() - 10,
				this.getHeight() / 20, mPaint);

		// draw the text
		mPaint.setARGB(200, 255, 255, 255);
		mPaint.setStrokeWidth(1);
		mPaint.setTextSize(22);
		mPaint.setTypeface(tf);
		canvas.drawText("Transportation Type Analysis", 30,	this.getHeight() * 1 / 20, mPaint);
		mPaint.setTypeface(tf);

		// draw the lines
		mPaint.setARGB(200, 255, 255, 255);
		mPaint.setStrokeWidth(1);
		canvas.drawLine(50, this.getHeight() * 3 / 20, this.getWidth() - 50,
				this.getHeight() * 3 / 20, mPaint);

		mPaint.setARGB(150, 175, 175, 175);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 4 / 20, this.getWidth() - 50,
				this.getHeight() * 4 / 20, mPaint);

		mPaint.setARGB(200, 255, 255, 255);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 5 / 20, this.getWidth() - 50,
				this.getHeight() * 5 / 20, mPaint);

		mPaint.setARGB(150, 175, 175, 175);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 6 / 20, this.getWidth() - 50,
				this.getHeight() * 6 / 20, mPaint);

		mPaint.setARGB(200, 255, 255, 255);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 7 / 20, this.getWidth() - 50,
				this.getHeight() * 7 / 20, mPaint);

		mPaint.setARGB(150, 175, 175, 175);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 8 / 20, this.getWidth() - 50,
				this.getHeight() * 8 / 20, mPaint);

		mPaint.setARGB(200, 255, 255, 255);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 9 / 20, this.getWidth() - 50,
				this.getHeight() * 9 / 20, mPaint);

		mPaint.setARGB(150, 175, 175, 175);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 10 / 20, this.getWidth() - 50,
				this.getHeight() * 10 / 20, mPaint);

		mPaint.setARGB(200, 255, 255, 255);
		// mPaint.setStrokeWidth(3);
		canvas.drawLine(50, this.getHeight() * 11 / 20, this.getWidth() - 50,
				this.getHeight() * 11 / 20, mPaint);

		// draw the text

		mPaint.setStrokeWidth(1);
		mPaint.setTextSize(16);

		
		int maxdisInt = (int) (maxdis);
		int oneQmax = (int) (maxdisInt*0.25);
		int halfmax = (int) (maxdisInt/2);
		int threeQmax = (int) (maxdisInt*0.75);
		
		
		String oneQmaxT = String.valueOf(oneQmax);
		String halfmaxT = String.valueOf(halfmax);
		String threeQmaxT = String.valueOf(threeQmax);
		String maxdisT = String.valueOf(maxdisInt);
		

		canvas.drawText(maxdisT, 10, this.getHeight() * 6 / 40, mPaint);

		canvas.drawText(threeQmaxT, 10, this.getHeight() * 10 / 40, mPaint);

		canvas.drawText(halfmaxT, 10, this.getHeight() * 14 / 40, mPaint);

		canvas.drawText(oneQmaxT, 10, this.getHeight() * 18 / 40, mPaint);

		canvas.drawText("0", 10, this.getHeight() * 22 / 40, mPaint);

		int wid_inter = (this.getWidth() - 100) / 6;

		// draw the numbers

		// mPaint.setTextSize(15);

		int day = -7;

		for (int i = 0; i < 7; i++) {
			day = day + 1;

			if (day == 0) {
				canvas.drawText(myDateF.format(myETime), 30 + wid_inter * i, this.getHeight() * 24 / 40, mPaint);

			} else if (day == -6) {
				canvas.drawText(myDateF.format(mySTime), 30 + wid_inter * i, this.getHeight() * 24 / 40, mPaint);

			} else if (day == -3) {
				canvas.drawText(myDateF.format(myMTime), 30 + wid_inter * i, this.getHeight() * 24 / 40, mPaint);
				

			}
			
			/*
			if (day == -1) {
				canvas.drawText("Today", 50 + wid_inter * i, this.getHeight() * 24 / 40, mPaint);

			} else if (day == 6) {
				canvas.drawText("yesterday", 50 + wid_inter * i, this.getHeight() * 24 / 40, mPaint);

			} else {
				canvas.drawText(day + "", 50 + wid_inter * i,
						this.getHeight() * 24 / 40, mPaint);

			}*/

		}

		// draw the underground thing
		// draw the types

		// Bitmap bmp =
		// BitmapFactory.decodeResource(getResources(),R.drawable.service_on);

		// canvas.drawBitmap(bmp,50 - 40, this.getHeight()*26/40,null);
		mPaint.setARGB(200, 0, 255, 255);
		mPaint.setTextSize(19);
		
		canvas.drawCircle(50 - 20, this.getHeight() * 27 / 39, 5, mPaint);
		canvas.drawText("BUS", 50, this.getHeight() * 28 / 40, mPaint);

		mPaint.setARGB(200, 173, 255, 47);
		// canvas.drawBitmap(bmp, 50+ 1.5f*wid_inter-40,
		// this.getHeight()*26/40,null);
		canvas.drawCircle(50 + 1.3f * wid_inter - 20,this.getHeight() * 27 / 39, 5, mPaint);
		canvas.drawText("TRAIN & TRAM", 50 + 1.3f * wid_inter, this.getHeight() * 28 / 40, mPaint);

		mPaint.setARGB(200, 255, 0, 0);
		// canvas.drawBitmap(bmp, 50+4*wid_inter-40,
		// this.getHeight()*26/40,null);
		canvas.drawCircle(50 + 4.1f * wid_inter - 20,this.getHeight() * 27 / 39, 5, mPaint);
		canvas.drawText("CAR", 50 + 4.1f * wid_inter,
				this.getHeight() * 28 / 40, mPaint);

		mPaint.setARGB(200, 255, 255, 255);
		// canvas.drawBitmap(bmp, 50+6*wid_inter-40,
		// this.getHeight()*26/40,null);
		canvas.drawCircle(50 + 5.4f * wid_inter - 20, this.getHeight() * 27 / 39,5, mPaint);
		canvas.drawText("WALK", 50 + 5.4f * wid_inter, this.getHeight() * 28 / 40,	mPaint);

		// canvas.drawBitmap(bmp, 50-40, this.getHeight()*28/40,null);
		// canvas.drawText("TOTAL", 50, this.getHeight()*30/40, mPaint);

		// canvas.drawCircle(screenPts.x, screenPts.y, 10, mPaint);
	}

	public void setData(DataProcessing d) {
		data = d;
	}

	public void drawData(Canvas canvas) {

		drawDdouble(canvas, data.walkdis, "walkdis");
		drawDdouble(canvas, data.busdis, "busdis");
		drawDdouble(canvas, data.cardis, "cardis");
		drawDdouble(canvas, data.traindis, "traindis");
		drawDdouble(canvas, data.totalDis, "totaldis");
	}

	private void drawDdouble(Canvas canvas, ArrayList<Double> numbers,
			String type) {
		// TODO Auto-generated method stub

		// this is for CIRCLE
		Paint mPaint = new Paint();
		mPaint.setDither(true);
		// now here, detect the type and change the color
		if (type.equalsIgnoreCase("walkdis")) {
			mPaint.setARGB(200, 255, 255, 255);
		} else if (type.equalsIgnoreCase("busdis")) {
			mPaint.setARGB(200, 0, 255, 255);
		} else if (type.equalsIgnoreCase("cardis")) {
			mPaint.setARGB(200, 255, 0, 0);
		} else if (type.equalsIgnoreCase("traindis")) {
			mPaint.setARGB(200, 173, 255, 47);
		} else if (type.equalsIgnoreCase("totaldis")) {
			mPaint.setARGB(200, 255, 0, 0);
		}

		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);

		// this is for LINE
		Paint mPaint1 = new Paint();
		mPaint1.setDither(true);
		// now here, detect the type and change the color
		if (type.equalsIgnoreCase("walkdis")) {
			mPaint1.setARGB(150, 255, 255, 255);

		} else if (type.equalsIgnoreCase("busdis")) {
			mPaint1.setARGB(150, 0, 0, 255);
		} else if (type.equalsIgnoreCase("cardis")) {
			mPaint1.setARGB(150, 255, 69, 0);
		} else if (type.equalsIgnoreCase("traindis")) {
			mPaint1.setARGB(150, 50, 205, 50);
		} else if (type.equalsIgnoreCase("totaldis")) {
			mPaint1.setARGB(150, 255, 255, 0);
		}

		mPaint1.setStrokeWidth(3);// LINE THIKNESS
		mPaint1.setStyle(Paint.Style.FILL);
		mPaint1.setStrokeJoin(Paint.Join.ROUND);
		mPaint1.setStrokeCap(Paint.Cap.ROUND);

		
		
		//the shared parameters, for example, the maximun value will be prepared here

		
	
		// now we started to draw something
		double maxdis = data.getMaxDis();//get max distance value
		int maxdisInt = (int) (maxdis);
		
		double hei_inter = this.getHeight() * (11 - 3) * 1.0 / (20 * maxdisInt);
		int wid_inter = (this.getWidth() - 100) / 6;

		if (numbers.size() > 0) {

			float pre = 0;

			for (int i = 0; i < numbers.size(); i++) {

				float d = (int) (this.getHeight() * 11 / 20 - numbers.get(i)
						* hei_inter);

				if (pre != 0) {
					canvas.drawLine(50 + wid_inter * (i - 1), pre, 50
							+ wid_inter * i, d, mPaint1);
				}

				canvas.drawCircle(50 + wid_inter * i, d, 4, mPaint); // CIRCLE
																		// SIZE
				// mPaint.setStyle(Paint.Style.STROKE);
				//
				// mPaint.setColor(Color.WHITE);
				// canvas.drawCircle(50 + wid_inter*i, d, 4, mPaint);

				pre = d;

			}

		}

	}

}
